package com.hui.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.reflect.TypeToken;
import com.hui.constants.MusicWebConstants;
import com.hui.domain.Mid;
import com.hui.domain.Song;
import com.hui.domain.Upload;
import com.hui.domain.User;
import com.hui.service.MusicWebService;
import com.hui.utils.JSONUtils;
import org.json.simple.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Ayingzz
 * @create 2021/12/25 16:29
 */
@RestController
public class MusicWebController {

    @Autowired
    private MusicWebService musicWebService;

    @Autowired
    private Upload upload;

    /**
     * 查询所有歌曲
     * @return
     */
    @RequestMapping(value = "/queryAllSongs", method = RequestMethod.GET)
    public List<Song> queryAllSongs() {
        List<Song> list = musicWebService.queryAllSongs();
        return list;
    }

    /**
     * 查询所有歌曲类型
     * @return
     */
    @RequestMapping(value = "/queryAllTypes", method = RequestMethod.GET)
    public Set<String> queryAllTypes() {
        Set<String> types = musicWebService.queryAllTypes();
        return types;
    }

    /**
     * 根据关键字查询歌曲
     * @param keyword
     * @return
     */
    @RequestMapping(value = "/querySongByKeyword", method = RequestMethod.GET)
    public List<Song> querySongByKeyword(String keyword) {
        List<Song> list = musicWebService.querySongByKeyword(keyword);
        return list;
    }

    /**
     * 根据歌曲id查询歌曲
     * @param mid
     * @return
     */
    @RequestMapping(value = "/querySongByMid", method = RequestMethod.GET)
    public Song querySongByMid(String mid) {
        System.out.println("mid:" + mid);
        Song song = musicWebService.querySongByMid(mid);
        System.out.println(song.toString());
        return song;
    }

    /**
     * 根据类型查询歌曲
     * @return
     */
    @RequestMapping(value = "/querySongByType", method = RequestMethod.GET)
    public List<Song> querySongByType(String type) {
        List<Song> list = musicWebService.querySongByType(type);
        return list;
    }

    /**
     * 根据用户名查询歌曲（我的喜欢）
     * @param uname
     * @return
     */
    @RequestMapping(value = "/querySongByUname", method = RequestMethod.GET)
    public List<Song> querySongByUname(String uname) {
        List<Song> list = musicWebService.querySongByUname(uname);
        return list;
    }

    /**
     * 收藏歌曲或取消收藏（我的喜欢）
     * @param uname
     * @return
     */
    @RequestMapping(value = "/addOrDeleteSongFromFavouriteListByUname", method = RequestMethod.GET)
    public String addOrDeleteSongFromFavouriteListByUname(String uname, String mid) {
        int count = musicWebService.addOrDeleteSongFromFavouriteListByUname(uname, mid);
        return count != 0 ? MusicWebConstants.DMLReturnCode.SUCCESSADD : MusicWebConstants.DMLReturnCode.SUCCESSOUT;
    }

    /**
     * 根据歌曲id增加播放次数
     * @param mid
     * @return
     */
    @RequestMapping(value = "/addPlayTimesByMid", method = RequestMethod.POST)
    public List<Song> addPlayTimesByMid(String mid) {
        List<Song> list = musicWebService.addPlayTimesByMid(mid);
        return list;
    }

    /**
     * 文件保存
     * @param imgFile
     * @param lyricFile
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    public String uploadFile(@RequestParam(value = "imgFile", required=false)MultipartFile imgFile,
                             @RequestParam(value = "lyricFile", required=false)MultipartFile lyricFile) throws IOException {
        if (imgFile != null) {
            upload.setImgFile(imgFile);
            System.out.println("图片装载成功");
        }
        if (lyricFile != null) {
            upload.setLyricFile(lyricFile);
            System.out.println("歌词装载成功");
        }
        return MusicWebConstants.DMLReturnCode.SUCCESS;
    }

    /**
     * 添加歌曲
     * @param songStr
     * @return
     */
    @RequestMapping(value = "/insertSong", method = RequestMethod.POST)
    public String insertSong(@RequestBody String songStr) throws IOException {
        int count = musicWebService.insertSong(songStr, upload);
        return count != 0 ? MusicWebConstants.DMLReturnCode.SUCCESS : MusicWebConstants.DMLReturnCode.FAIL;
    }

    /**
     * 根据歌曲id编辑歌曲
     * @param songStr
     * @return
     */
    @RequestMapping(value = "/updateSongByMid", method = RequestMethod.POST)
    public String updateSongByMid(@RequestBody String songStr) throws JsonProcessingException {
        int count = musicWebService.updateSongByMid(songStr);
        return count != 0 ? MusicWebConstants.DMLReturnCode.SUCCESS : MusicWebConstants.DMLReturnCode.FAIL;
    }

    /**
     * 根据id删除歌曲
     * @param mid
     * @return
     */
    @RequestMapping(value = "/deleteSongByMid", method = RequestMethod.POST)
    public String deleteSongByMid(@RequestBody String mid) {
        int count = musicWebService.deleteSongByMid(mid);
        return count != 0 ? MusicWebConstants.DMLReturnCode.SUCCESS : MusicWebConstants.DMLReturnCode.FAIL;
    }

    /**
     * 增加一个用户
     * @param userStr
     * @return
     */
    @RequestMapping(value = "/insertUser", method = RequestMethod.POST)
    public User insertUser(@RequestBody String userStr) throws JsonProcessingException {
        User user = musicWebService.insertUser(userStr);
        return user;
    }

    /**
     * 匹配用户（登录）
     * @param uname
     * @param upass
     * @return
     */
    @RequestMapping(value = "/checkUserByUname", method = RequestMethod.GET)
    public String checkUserByUname(String uname, String upass) {
        String unameRes = musicWebService.checkUserByUname(uname, upass);
        return unameRes;
    }

}
